require "capistrano/recipes/deploy/scm"

require "wondercap/lib/deployment/model/area"
require "wondercap/lib/deployment/model/deployment"
require "wondercap/lib/deployment/model/snapshot"

require "wondercap/lib/deployment/core/local_build_repository"
require "wondercap/lib/deployment/core/remote_deployment_repository"

require "fileutils"

extension :base => :base do
  set(:area) { outer_configuration.area }
  set(:build_repository) { repo = Wondercap::Deployment::LocalProjectRepository.new(project_name, "/usr/local/wondercap/build"); repo.sync; repo }
  set(:deployment_repository) { Wondercap::Deployment::RemoteProjectRepository.new(project_name, "/usr/local/wondercap/dep") }
  set(:revision_build) { build_repository[real_revision] }
  set(:revision_deployment) { deployment_repository[real_revision] }
  set(:installed_revision_deployment) do
    cur_snap = area.current_snapshot
    if cur_snap
      dep = cur_snap.find_deployment_by_name(project_name)
      deployment_repository[dep.revision] if dep
    end    
  end
  
  set :scm, nil
  task :fill_scm, :on_first_fetch => :scm do
  end
  
  set :real_revision, nil
  task :fill_real_revision, :on_first_fetch => :real_revision do
  end
  
  set :runtime_dependencies, []
  task :fill_runtime_dependencies, :on_first_fetch => :runtime_dependencies do
  end
  
  set :deployment_list, Wondercap::Deployment::DeploymentList.new
  task :fill_deployment_list, :on_first_fetch => :deployment_list do
  end
end
